﻿using UnityEngine;
using System.Collections;

public class Popup_Store_Get : Popup_Base {
	public UILabel charNameLabel;
	public UISprite[] traitIconList;
	public UILabel[] traitNameLabelList;
	public UILabel[] reAttemptCostLabel;

	public GameObject[] rareStar;
	public GameObject[] unfixTraitObject;
	public GameObject[] fixTraitObject;

	public GameObject char3DModel;
    public Transform PCCharacterPosition;
    public Transform NPCCharacterPosition;

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
	
	}

	int _charId, _additionalTraitId1, _additionalTraitId2 = 0;
	public void SetInfos(int charId, int additionalTraitId1, int additionalTraitId2)
	{	
		_charId = charId;
		_additionalTraitId1 = additionalTraitId1;
		_additionalTraitId2 = additionalTraitId2;
		DStoreCharacter charInfo = GameData.Inst.GetCharDrawItemInfo(charId);

		charNameLabel.text = string.Format("Lv{0}.{1}", charInfo.lv.ToString(), TextController.GetStringWithId(string.Format("charName{0}", charInfo.nameID)));
        foreach(UILabel label in reAttemptCostLabel)
            label.text = GameData.Inst.GetStoreGoodInfo(charId).againPrice.ToString();

        string modelName = GameData.Inst.ModelDefineDic[charInfo.modelNo];
        GameObject m_3DModel = BattleResManager.CreatePawn(0,charInfo.modelNo, false);
        m_3DModel.transform.parent = PCCharacterPosition;
        for (int i = 0; i < NPCCharacterPosition.childCount; i++ )
        {
            Transform trans = NPCCharacterPosition.GetChild(i);
            if (modelName.ToLower().Contains(trans.gameObject.name))
            {
                m_3DModel.transform.parent = trans;
            }
        }
        m_3DModel.transform.localPosition = Vector3.zero;
        m_3DModel.transform.localRotation = Quaternion.Euler(Vector3.zero);
        m_3DModel.transform.localScale = Vector3.one;
        SceneController.SetAllChildsLayer(m_3DModel.transform, m_3DModel.transform.parent.gameObject.layer);

        var ani = m_3DModel.MakeComponent<Animation>();
        ani["lobby01"].clip.wrapMode = WrapMode.Loop;
        ani.Play("lobby01");

		traitIconList[0].spriteName = charInfo.trait1Value.ToString();
		traitIconList[1].spriteName = charInfo.trait2Value.ToString();
		traitIconList[2].spriteName = charInfo.trait3Value.ToString();
		traitNameLabelList[0].text = TextController.GetStringWithId(string.Format("trait{0}", charInfo.trait1Value));
		traitNameLabelList[1].text = TextController.GetStringWithId(string.Format("trait{0}", charInfo.trait2Value));
		traitNameLabelList[2].text = TextController.GetStringWithId(string.Format("trait{0}", charInfo.trait3Value));

		if( additionalTraitId1 != 0 )
		{
			traitIconList[3].spriteName = additionalTraitId1.ToString();
			traitNameLabelList[3].text = TextController.GetStringWithId(string.Format("trait{0}", additionalTraitId1));
            rareStar[2].SetActive(false);
            rareStar[3].SetActive(true);
		}
		else
		{
			fixTraitObject[3].SetActive(false);
//			unfixTraitObject[3].SetActive(true);
		}
		if( additionalTraitId2 != 0 )
		{
			traitIconList[4].spriteName = additionalTraitId2.ToString();
			traitNameLabelList[4].text = TextController.GetStringWithId(string.Format("trait{0}", additionalTraitId2));
            rareStar[2].SetActive(false);
            rareStar[3].SetActive(false);
            rareStar[4].SetActive(true);
		}
		else
		{
			fixTraitObject[4].SetActive(false);
//			unfixTraitObject[4].SetActive(true);
		}
	}

	public void OnClickOK()
	{
		PopupManager.PopPopup();
	}

	public void OnClickReTry()
	{
		NetworkManager.Instance.C2S_BUY_STORE_ITEM(ResponseBuyCharDrawItem, _charId, true);		
	}

	private void ResponseBuyCharDrawItem(ResponseModel<BuyStoreItemResultModel> result)
	{
		Debug.LogError(result);
		if( result.ecode != 0 )
		{
			Popup_OneButton po = PopupManager.PushPopup(PopupKind.OneButton) as Popup_OneButton;
			po.SetPopupWithTitleAndBody(TextController.GetStringWithId("uiCommonTextwarning"), TextController.GetStringWithId(string.Format("systemErrorText{0}", Mathf.Abs(result.ecode))));
			return;
		}

        UserData.Instance.userModel.winPoint -= (short)GameData.Inst.GetStoreGoodInfo(_charId).againPrice;
        CharacterDrawViewManager.instance.pointAmountLabel.text = UserData.Instance.userModel.winPoint.ToString();

		PopupManager.PopPopup();
		Popup_Store_SlotSpin2 popup = PopupManager.PushPopup(PopupKind.Popup_Store_SLotSpin) as Popup_Store_SlotSpin2;
		popup.SetPopupInfos(_charId, result.data.member.traits[3], result.data.member.traits[4]);
		
	}
}
